/*
 * @Author: zhangfeng
 * @Date: 2023-12-27 13:41:21
 * @LastEditTime: 2023-12-27 15:02:43
 * @LastEditors: zhangfeng
 * @Description:
 * @FilePath: \nodeapi\src\controller\role.controller.js
 * © 2023 张峰，All rights reserved.
 */
const {
  createRoleServer,
  getRoleAllServer,
  updateRoleServer,
  deleteRoleServer,
} = require("../server/role.server");
const {
  roleCreateFormatterError,
  roleGetFormatterError,
  roleUpdateFormatterError,
  roleDeleteFormatterError,
} = require("../constants/err.type");
class RoleController {
  // 添加角色信息
  async createRole(ctx, next) {
    const { name, description } = ctx.request.body;
    console.log(name);
    console.log(description);
    try {
      const res = await createRoleServer({ name, description });
      if (res) {
        ctx.body = {
          code: 0,
          message: "创建角色成功",
          result: "",
        };
      } else {
        ctx.body = roleCreateFormatterError;
      }
    } catch (error) {
      console.log(error);
      ctx.body = roleCreateFormatterError;
    }
  }
  // 获取所有角色信息
  async getRoleAll(ctx, next) {
    try {
      const result = await getRoleAllServer();
      if (result) {
        ctx.body = {
          code: 0,
          message: "获取角色列表成功",
          result: result,
        };
      } else {
        ctx.body = roleGetFormatterError;
      }
    } catch (error) {
      console.error(error);
      ctx.body = roleGetFormatterError;
    }
  }
  // 修改角色信息
  async updateRole(ctx, next) {
    console.log(ctx.request.body);
    try {
      const result = await updateRoleServer(ctx.request.body);
      console.log(result);
      if (result) {
        ctx.body = {
          code: 0,
          message: "修改角色信息成功",
          result: "",
        };
      } else {
        ctx.body = roleUpdateFormatterError;
      }
    } catch (error) {
      ctx.body = roleUpdateFormatterError;
    }
  }

  // 删除角色信息
  async deleteRole(ctx, next) {
    try {
      const result = await deleteRoleServer(ctx.request.body.id);
      if (result) {
        ctx.body = {
          code: 0,
          message: "删除角色信息成功",
          result: "",
        };
      } else {
        ctx.body = roleDeleteFormatterError;
      }
    } catch (error) {
      console.error(error);
      ctx.body = roleDeleteFormatterError;
    }
  }
}

module.exports = new RoleController();
